home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / ada / adaed-1.11 / adaed-1 / Adaed-1.11.0a / imisc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-07  |  2.9 KB  |  109 lines

  1. /*
  2.  * Copyright (C) 1985-1992  New York University
  3.  * 
  4.  * This file is part of the Ada/Ed-C system.  See the Ada/Ed README file for
  5.  * warranty (none) and distribution info and also the GNU General Public
  6.  * License for more details.
  7.  
  8.  */
  9. #include "ipredef.h"
  10. #include "time.h"
  11. #include "imiscprots.h"
  12.  
  13. /* Procedures reset_clock and itime are used to maintain the 'clock'.
  14.  * reset_now resets the clock, itime returns the number of milliseconds
  15.  * since the clock was reset. Code is include to make it appear as though
  16.  * the clock was reset at the most recent midnight, in order that the
  17.  * addition of TIME and DURATION values can account for the
  18.  * overflow that occurs at midnight.
  19.  */
  20.  
  21. static long since_midnight;    /* milliseconds since midnight at start */
  22. static long start_time;    /* starting time (units vary according to version) */
  23.  
  24. /* set TIME_KIND to determine which itime and reset_clock to use */
  25.  
  26. #define TIME_DEFAULT
  27.  
  28. #ifdef BSD
  29. #undef TIME_DEFAULT
  30. #define TIME_BSD
  31. /* start_time is in seconds for BSD */
  32. #include <sys/time.h>
  33. static int tzpa[2];
  34. static long tpa[2];
  35. #endif
  36.  
  37. #ifdef IBM_PC
  38. #undef TIME_DEFAULT
  39. #define TIME_PC
  40. /* start_time is in 'ticks' elapsed since midnight, where there are 
  41.  * CLK_TCK ticks per second.
  42.  * To avoid overflow, we use ms_per_tick, milliseconds per tick. This
  43.  * assumes (as is currently the case) that CLK_TCK divides 1000
  44.  * evenly.
  45.  */
  46. #define ms_per_tick (1000 / CLK_TCK)
  47. #endif
  48.  
  49.  
  50. void reset_clock()                                            /*;reset_clock*/
  51. {
  52.     /* set start_time and since_midnight as described above. */
  53. #ifdef TIME_BSD
  54.     struct tm *t;
  55.     struct timeval *tp;
  56.  
  57.     tp = (struct timeval *)tpa;
  58.     gettimeofday(tp, (struct timezone *)tzpa);
  59.     start_time = tp->tv_sec;
  60.     t = localtime(&tp->tv_sec); /* break into hours, minutes, etc. */
  61.     since_midnight = (t->tm_hour * 3600 + t->tm_min * 60 + t->tm_sec)
  62.       * 1000L; /* milliseconds since midnight */
  63. #endif
  64. #ifdef TIME_PC
  65.     start_time = clock() * ms_per_tick; /* milliseconds since midnight */
  66.     since_midnight = start_time;
  67. #endif
  68. #ifdef TIME_DEFAULT
  69. #ifdef TBSN
  70.     /* adjustment for midnight still needed */
  71.     chaos("reset_clock for  TIME_DEFAULT not implemented");
  72. #endif
  73.     start_time = time(0);
  74. #endif
  75. }
  76.  
  77. long itime()                             /*;itime*/
  78. {
  79.     /* find elapsed seconds, convert to milliseconds, and add offset 
  80.      *  for midnight corresponding to desired origin
  81.      */
  82.     long  itim;
  83. #ifdef TIME_DEFAULT
  84. #ifdef TBSN
  85.     chaos("itime for TIME_DEFAULT not implemented");
  86. #endif
  87.     return (time(0) - start_time) * 1000;
  88. #endif
  89.  
  90. #ifdef TIME_BSD
  91.     struct timeval *tp;
  92. #endif
  93. #ifdef TIME_PC
  94.     clock_t time_now;
  95. #endif
  96. #ifdef TIME_BSD
  97.     tp = (struct timeval *)tpa;
  98.     gettimeofday(tp, (struct timezone *)tzpa);
  99.     itim = ((tp->tv_sec - start_time)*1000L + tp->tv_usec/1000L);
  100. #endif
  101. #ifdef TIME_PC
  102.     time_now = clock() * (long) ms_per_tick;
  103.     /* adjust for passing midnight if necessary */
  104.     if (time_now < start_time) time_now = time_now + 86400000L ;
  105.     itim =   (long) (time_now - start_time);
  106. #endif
  107.     return itim + since_midnight;
  108. }
  109.